const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    zhRadio:true,
    wxRadio: false,
    payDetails:false,
    servicePrice:'',
    taskTitle:'',
    balanceVal:'',
    task_id:'',
    passwordVal:'',
    payPasswordBox:false,
    taskDetail:'',
    serviceTextList:[],
    code:'',
    section_id:'',
    taskPay:'',//招标任务托管支付
  },
  // 显示明细
  payDetailsShow:function(){
    var { payDetails } = this.data
    this.setData({
      payDetails: !payDetails
    })
  },
  // 隐藏明细
  payDetailsHide: function () {
    var { payDetails } = this.data
    this.setData({
      payDetails: !payDetails
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let pages = getCurrentPages();
    let prevPage = pages[pages.length - 2];
    let pageData = prevPage.data;
    var taskType = '';
    if (pageData.taskType == 1) {
      taskType = 'zhaobiao'
    } else {
      taskType = 'xuanshang'
    }

    console.log(options)
    if (options.serviceTextList){
      var serviceTextList = JSON.parse(options.serviceTextList)
    }
    if(options.section_id){
      this.setData({
        section_id:options.section_id
      })
    }
    this.setData({
      servicePrice: options.servicePrice,
      taskTitle: options.title,
      task_id: options.taskid,
      serviceTextList,
      code: options.code,
      taskPay:options.taskPay
    })
    // 钱包余额
    this.balance();
    // 任务明细
    this.taskDetail();
  },
  // 支付选择
  radioChange:function(e){
    var { wxRadio, zhRadio } = this.data
    this.setData({
      wxRadio: !wxRadio,
      zhRadio: !zhRadio
    })
  },
  // 钱包余额
  balance:function(){
    var _this = this;
    wx.request({
      url: app.globalData.baseUrl + 'pay/balance',
      data: {
        token: app.globalData.token
      },
      success(res) {
        console.log(res)
        if (res.data.code == 1000) {
          _this.setData({
            balanceVal: res.data.data.balance
          })
        } else {
          wx.showToast({
            title: res.data.message,
            icon: 'none',
            duration: 2000
          })
        }
      }
    })
  },
  // 任务详情信息
  taskDetail: function () {
    var _this = this;
    var { task_id, servicePrice } = this.data;
    wx.request({
      url: app.globalData.baseUrl + 'myTask/detail',
      data: {
        id: task_id,
        token: app.globalData.token
      },
      success(res) {
        wx.hideLoading()
        console.log(res)
        if (res.data.code == 1000) {
          _this.setData({
            taskDetail: res.data.data
          })
          if (res.data.data.task_type == 'xuanshang'){
            _this.setData({
              servicePrice: Number(servicePrice) + Number(res.data.data.bounty)
            })
          }
        } else {
          wx.showToast({
            title: res.data.message,
            icon: 'none',
            duration: 2000
          })
        }
      }
    })
  },
  // 支付
  submitPay:function(){
    var { zhRadio, payPasswordBox,taskPay } = this.data
    var _this = this;
    if (zhRadio){
      this.setData({
        payPasswordBox: !payPasswordBox
      })
    }else{
      var _this = this
      var url = app.globalData.baseUrl + 'pay/wechatPay1';
      var { code } = this.data;
      var data = {
        code: code,
        token: app.globalData.token
      }
      wx.showLoading({
        title: '加载中',
      })
      app.wxPostRequest(url, data, (res) => {
        console.log(res)
        wx.hideLoading()
        if (res.code == 1000) {
          wx.requestPayment({
            timeStamp: String(res.data.timeStamp),
            nonceStr: String(res.data.nonce_str),
            package: String(res.data.package),
            signType: 'MD5',
            paySign: String(res.data.paySign),
            success(payres) {
              if(taskPay == 1){
                wx.navigateBack({
                  delta: 1
                })
              }else{
                wx.reLaunch({
                  url: '/pages/index/index',
                })
              }
            },
            fail(payres) {
              console.log(payres)
            }
          })
        }
      }, (err) => {
        wx.hideLoading()
        wx.showToast({
          title: err.data.message,
          icon: 'none',
          duration: 2000
        })
      })
    }
  },
  // 支付密码
  payPasswordInp:function(e){
    this.setData({
      passwordVal:e.detail.value
    })
  },
  // 取消
  cancelPay:function(){
    var { payPasswordBox } = this.data;
    this.setData({
      payPasswordBox: !payPasswordBox
    })
  },
  // 确定
  determinePay: function () {
    var { task_id, passwordVal,section_id,taskPay } = this.data;
    if (passwordVal ==''){
      wx.showToast({
        title: '请输入支付密码',
        icon: 'none',
        duration: 2000
      })
      return false
    }
    wx.showLoading({
      title: '加载中',
    })
    wx.request({
      url: app.globalData.baseUrl + 'pay/bountyByBalance',
      data: {
        task_id: task_id,
        pay_type: 0,
        password: passwordVal,
        token: app.globalData.token,
        section_id:section_id
      },
      method: "POST",
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      success(res) {
        wx.hideLoading()
        console.log(res)
        if (res.data.code == 1000) {
          wx.showToast({
            title: '支付成功',
            icon: 'success',
            duration: 2000
          })
          setTimeout(function () {
            if(taskPay == 1){
              wx.navigateBack({
                delta: 1
              })
            }else{
              wx.reLaunch({
                url: '/pages/index/index',
              })
            }
          }, 2000)
        } else {
          wx.showToast({
            title: res.data.message,
            icon: 'none',
            duration: 2000
          })
        }
      }
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})
